// Class include
#include "CCommandsDispatcher.hpp"

// System includes

// Other includes
#include "CTrace.hpp"

// Forward declaration inckudes
#include "CCommand.hpp"

CCommandsDispatcher* CCommandsDispatcher::mSelf = 0;

CCommandsDispatcher::CCommandsDispatcher()
: CObserver()
, mpSocketPtr(0)
, mpSerialPtr(0)
{
}

CCommandsDispatcher::~CCommandsDispatcher()
{
}

void CCommandsDispatcher::setReaderPointers(CSocketReader* pSocketPtr, CSerialReader* pSerialPtr)
{
   mpSocketPtr = pSocketPtr;
   mpSerialPtr = pSerialPtr;
}

void CCommandsDispatcher::handleEvent(const CCommand& cmd)
{
   if (COMMAND_SOURCE_ARDUINO == cmd.getSource())
   {
      if (0 != mpSocketPtr)
      {
         mpSocketPtr->sendCommand(cmd);
      }
      else
      {
         TRC_ERROR("CCommandsDispatcher::sendCommand: socketReader object is NULL");
      }
   } else if (COMMAND_SOURCE_SOCKET == cmd.getSource())
   {
      if (0 != mpSerialPtr)
      {
         mpSerialPtr->sendCommand(cmd);
      }
      else
      {
         TRC_ERROR("CCommandsDispatcher::sendCommand: arduinoReader object is NULL");
      }
   }
}

CCommandsDispatcher* CCommandsDispatcher::getInstance()
{
   if (mSelf == 0)
   {
      mSelf = new CCommandsDispatcher();
   }

   return mSelf;
}